//
//  LoginViewController.m
//  test
//
//  Created by 朱圳 on 2017/12/21.
//  Copyright © 2017年 朱圳. All rights reserved.
//

#import "JWLoginViewController.h"
#import "LoginView.h"
#import "JWZHMainTabar.h"
#import "JWUserModel.h"
#import "JWUserManager.h"
@interface JWLoginViewController ()<LoginViewDelegate>
@property(nonatomic,strong)LoginView *loginView;
@property(nonatomic,strong)JWUserModel *user;
@end

@implementation JWLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.loginView];
  //  self.title = @"登录";
    //去黑线
    self.navigationController.navigationBar.clipsToBounds = YES;
    // 设置初始导航栏透明度
   // [self wr_setNavBarBackgroundAlpha:0];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(LoginView *)loginView{
    
    if (!_loginView) {
        _loginView = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        _loginView.delegate = self;
    
    }
    return _loginView;
}

- (void)forgetButton:(UIButton *)sender {

}

- (void)loginButton:(UIButton *)sender withTel:(NSString *)tel andPassword:(NSString *)password {
    BOOL isTel = [self validateMobile:tel];
    if (isTel == YES &&password.length>=3) {
        NSDictionary *dict = @{
                               @"phone":tel,
                               @"password":password,
                               @"type":@"3",
                               };

        [[AFNetworkTool shareManager]postJSONWithUrl:Login parmas:dict successData:^(id json) {
                if ([json[@"messagetype"] isEqual:@"1"]) {
  //         NSLog(@"-教务-json--%@", json);
               self.user = [JWUserModel yy_modelWithJSON:json];
               [JWUserManager save:self.user];
                    [[NSUserDefaults standardUserDefaults] setObject:tel forKey:@"rememberTel"];
                    
                    [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"rememberpassword"];
                    
                    JWZHMainTabar *tabBarControllerConfig = [[JWZHMainTabar alloc] init];
                    [[UIApplication sharedApplication].delegate.window setRootViewController:tabBarControllerConfig];
//                    [Utility showSuccess:@"成功"];
                }else{
//                    [Utility showFail:@"账号密码错误"];
                }
        } failure:^(NSError *error) {
            NSLog(@"%@", error);
        }];
       
    }else{
      // [Utility showFail:@"电话格式不对或者密码小于3位"];
    }
    
}

- (void)registButton:(UIButton *)sender {
    

}

- (BOOL)validateMobile:(NSString *)mobile
{
    // 手机号以13、15、18、17开头，八个 \d 数字字符
    NSString * phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$";
    NSPredicate * phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
@end
